<?php

namespace app\common\model;

use app\common\model\BaseModel;


class ServiceBasic extends BaseModel
{

    // 表名
    protected $name = 'service_basic';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];


    public function getStatusList()
    {
        return ['normal' => '正常', 'trash' => '停用', 'deleted' => '已删除'];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBasePriceAttr($value, $data)
    {
        return $value ? json_decode($value,true) : [];
    }
    public function getHourPriceAttr($value, $data)
    {
        return $value ? json_decode($value,true) : [];
    }
    public function getDayPrice1Attr($value, $data)
    {
        return $value ? json_decode($value,true) : [];
    }
    public function getDayPrice2Attr($value, $data)
    {
        return $value ? json_decode($value,true) : [];
    }
    public function getDayPrice3Attr($value, $data)
    {
        return $value ? json_decode($value,true) : [];
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getContentAttr($value, $data)
    {
        return $value===null ? '' : $value;
    }
    public function getNoticeAttr($value, $data)
    {
        return $value===null ? '' : $value;
    }
    public function getOrderNoticeAttr($value, $data)
    {
        return $value===null ? '' : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public static function realPrice(&$service,$channel){
        $service['base_price']=self::findPrice($service['base_price'],$channel);
        $service['hour_price']=self::findPrice($service['hour_price'],$channel);
        $service['day_price1']=self::findPrice($service['day_price1'],$channel);
        $service['day_price2']=self::findPrice($service['day_price2'],$channel);
        $service['day_price3']=self::findPrice($service['day_price3'],$channel);
    }
    public static function findPrice($price_arr,$channel){
        if(!is_array($price_arr)&&$price_arr!=''){
            $price_arr=json_decode($price_arr,true)?:[];
        }
        if(!$price_arr){
            return 0;
        }

        if(isset($price_arr[$channel])){
            return $price_arr[$channel];
        }
        else{
            if(isset($price_arr['0'])){
                return $price_arr['0'];
            }
            else{
                return 0;
            }
        }
    }

}
